/**
 * Created by Administrator on 2017/2/28 0028.
 */

function student(name, code) {
    this.name = name;
    this.code = code;
}

function JsonOBJ() {
    this.id = null;
    this.majorname = null;
    this.classname = null;
    this.studentname = null;
    this.startTime = null;
    this.endTime = null;
    this.teachername = null;
    this.requestReason = null;
    this.rejectReason = null;
    this.authStatus = null;
    this.requestTime = null;
}

var isSubmit = 0;

$(function () {
    setDataOfStuList();
    $(".apply-replace-we").click(function () {
        if(isSubmit==0){
            replaceList();
        }else {
            layer.msg("请勿重复提交", {time: 1000});
        }
    });
});

function setDataOfStuList() {
    $(".replace-name-list").empty();
    var str = '';
    var stu = sessionStorage.getItem("checkedStu").split(",");
    var students = new Array();
    for (var i = 0; i < stu.length / 2; i++) {
        students.push(new student(stu[i * 2], stu[i * 2 + 1]));
    }

    for (var i = 0; i < students.length; i++) {
        str += '<li><div class="fl"><img src="../../../images/img_xuanzhe_02.png" alt="" check_type=1 id="' +
            students[i].code + '" name="' +
            students[i].name + '"></div><span>' +
            students[i].name + '</span></li>';

        // str += '<li class="col-sm-3 student-name"><ul class="student-area"><li class="fl checkBox margin-cancel">' +
        //     '<img src="../../../images/img_xuanzhe_01.png" alt="" class="check" check_type=0 id=' +
        //     students[i].code + '></li><li><h4 class="name-border">' +
        //     students[i].name + '<br/>' +
        //     students[i].code + '</h4></li></ul></li>'
    }

    if (students.length % 3 == 1) {
        str += '<li><div class="fl" style="visibility: hidden"><img src="../../../images/img_xuanzhe_02.png" alt=""></div><span></span></li>' +
            '<li><div class="fl" style="visibility: hidden"><img src="../../../images/img_xuanzhe_02.png" alt=""></div><span></span></li>';
    } else if (students.length % 3 == 2) {
        str += '<li><div class="fl" style="visibility: hidden"><img src="../../../images/img_xuanzhe_02.png" alt=""></div><span></span></li>';
    }

    $(".replace-name-list").html(str);
}

function checkInput() {
    var st = $("#date-start").val();
    var et = $("#date-end").val();
    var reason = $("#apply-reason").val();
    if (st != "" && et != "") {

        if (reason != "") {
            var sTime = new Date(st.replace("T", " "));
            var eTime = new Date(et.replace("T", " "));
            var nTime = new Date();
            if (sTime <= nTime) {
                layer.msg("起始日期不能早于当前时间", {time: 1000});
                console.log(sTime);
                console.log(nTime);
                console.log(eTime);
                return 3;
            } else if (sTime >= eTime) {
                layer.msg("结束日期不能早于起始日期", {time: 1000});
                console.log(sTime);
                console.log(nTime);
                console.log(eTime);
                return 4;
            } else {
                return 0;
            }
        } else {
            layer.msg("请假原因不能为空", {time: 1000});
            return 2;
        }
    } else {

        layer.msg("日期不能为空", {time: 1000});
        return 1;
    }
}

function replaceList() {
    if (checkInput() == 0) {
        var checkIds = new Array();
        $("img[id]").each(function () {
            if ($(this).attr("check_type") == 1) {
                var js = new JsonOBJ();
                js.studentname = $(this).attr("id");
                js.requestReason = $("#apply-reason").val();
                js.startTime = new Date($("#date-start").val().replace("T", " "));
                js.endTime = new Date($("#date-end").val().replace("T", " "));
                checkIds.push(js);
            }
        });
        $.ajax({
            url: "/huaqiao/front/teacher/replace?" + new Date().getTime(),
            type: "post",
            data: JSON.stringify(checkIds),
            dataType: "json",
            contentType: "application/json; charset=utf-8",
            async: false,
            success: function (str) {
                if (check(str)) {
                    isSubmit = 1;
                    if (checkIds.length != parseInt(str)) {
                        layer.msg("失败" + (checkIds.length - parseInt(str)) + "条", {time: 1500});
                    } else {
                        layer.msg("代发成功", {time: 1500});
                    }
                    setTimeout(function () {
                        window.location = 'apply.weReplace_teacher.html';
                    }, 1500);
                }
            }
        });
    }
}

function check(str){
    if (str != null && str != "") {
        if (str.errorcode != undefined) {
            if (str.errorcode == 1) {
                if (islogin == 1) {
                    layer.msg(str.errormessage,{time:1000});
                    // window.location.href = "/huaqiao/front_end/pc/teacher/login_pc_teacher.html";
                    islogin = 0;
                }
            }
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}